package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.mapper.IsolationPlaceMapper;
import com.lnking.lnkingplace.mapper.IsolationRoomMapper;
import com.lnking.lnkingplace.model.dto.isolation.room.PersonDto;
import com.lnking.lnkingplace.model.dto.isolation.room.RoomCount;
import com.lnking.lnkingplace.model.dto.isolation.room.RoomDetailImport;
import com.lnking.lnkingplace.model.dto.isolation.room.RoomListDto;
import com.lnking.lnkingplace.model.entity.IsolationRoom;
import com.lnking.lnkingplace.model.enums.DateFormat;
import com.lnking.lnkingplace.model.enums.RoomType;
import com.lnking.lnkingplace.model.request.isolation.room.RoomAddRequest;
import com.lnking.lnkingplace.model.request.isolation.room.RoomUpdateRequest;
import com.lnking.lnkingplace.model.request.isolation.room.RoomUpdateStatusRequest;
import com.lnking.lnkingplace.service.IIsolationRoomService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-27
 */
@Service
public class IsolationRoomServiceImpl extends CustomServiceImpl<IsolationRoomMapper, IsolationRoom> implements IIsolationRoomService {

    @Resource
    UserUtil userUtil;
    @Resource
    IsolationPlaceMapper isolationPlaceMapper;

    @Override
    public IsolationRoom getRoomInfo(String placeName, String roomNumber) {
        String placeId = isolationPlaceMapper.getPlaceId(placeName);
        LambdaQueryWrapper<IsolationRoom> query = new LambdaQueryWrapper<>();
        query.eq(IsolationRoom::getPlaceId,placeId);
        query.eq(IsolationRoom::getRoomNumber,roomNumber);
        return this.baseMapper.selectOne(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(String placeId, MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<RoomDetailImport> result =  ImportUtils.importExcel(file, RoomDetailImport.class,option);
        for(RoomDetailImport dto : result){
            if(StringUtils.isNotBlank(dto.getRoomNumber())){
                IsolationRoom info = new IsolationRoom();
                BeanUtils.copyProperties(dto,info);
                info.setUpdateTime(LocalDateTime.now());
                if(StringUtils.isNotBlank(dto.getStartDate())){
                    info.setStartDate(LocalDateTimeUtil.stringToLocalDate(dto.getStartDate(), DateFormat.LOCAL_DATE));
                }
                if(StringUtils.isNotBlank(dto.getEndDate())){
                    info.setEndDate(LocalDateTimeUtil.stringToLocalDate(dto.getEndDate(), DateFormat.LOCAL_DATE));
                }
                placeId = isolationPlaceMapper.getPlaceId(dto.getPlaceName());
                Assert.isTrue(StringUtils.isNotBlank(placeId), "该场所不存在："+"【"+dto.getPlaceName()+"】");
                info.setPlaceId(placeId);
//                IsolationRoom check = check(placeId, info.getRoomNumber());
//                if(null == check){
//                    this.baseMapper.insert(info);
//                }
//                else {
//                    info.setId(check.getId());
//                    this.baseMapper.updateById(info);
//                }
                this.baseMapper.insert(info);
            }
        }
    }

    private String getPlaceId(String placeId){
        if(StringUtils.isBlank(placeId)){
            String name = userUtil.getUser().getName();
            return isolationPlaceMapper.getPlaceId(name);
        }
        return placeId;
    }


    @Override
    public void add(RoomAddRequest params) throws Exception {
        //验证是否存在该房间
        IsolationRoom check = check(params.getPlaceId(),params.getRoomNumber());
        Assert.isNull(check,"该房间号已存在");
        IsolationRoom info = new IsolationRoom();
        BeanUtils.copyProperties(params,info);
        info.setUpdateTime(LocalDateTime.now());
        info.setUseStatus(RoomType.UNUSE);
        this.baseMapper.insert(info);

    }

    private IsolationRoom check(String placeId,String roomNumber){
        LambdaQueryWrapper<IsolationRoom> query = new LambdaQueryWrapper<>();
        query.eq(IsolationRoom::getRoomNumber,roomNumber).eq(IsolationRoom::getPlaceId,placeId);
        IsolationRoom info = this.baseMapper.selectOne(query);
        return info;
    }

    @Override
    public void update(RoomUpdateRequest params) throws Exception {
        //验证是否存在该房间
        IsolationRoom check = check(params.getPlaceId(),params.getRoomNumber());
        if(null != check){
            Assert.isTrue(check.getId().equals(params.getId()),"该房间号已存在");
        }
        IsolationRoom info = new IsolationRoom();
        BeanUtils.copyProperties(params,info);
        info.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(info);
    }

    @Override
    public void updateStatus(List<RoomUpdateStatusRequest> params) {
        for (RoomUpdateStatusRequest data : params){
            IsolationRoom info = new IsolationRoom();
            BeanUtils.copyProperties(data,info);
            info.setUpdateTime(LocalDateTime.now());
            this.baseMapper.updateById(info);
        }
    }

    @Override
    public PersonDto queryPerson(String placeId, String roomNumber) {
        return this.baseMapper.queryPerson(placeId,roomNumber);
    }

    @Override
    public void updateData() {
        List<RoomListDto> list = this.baseMapper.queryAll();
        list.forEach(result ->{
            String roomNumber = result.getRoomNumber();
            String placeId = result.getPlaceId();
            PersonDto personDto = this.baseMapper.queryPerson(placeId, roomNumber);
            if(null != personDto){
                result.setUseStatus(RoomType.USEING);
                IsolationRoom info = new IsolationRoom();
                BeanUtils.copyProperties(result,info);
                this.baseMapper.updateById(info);
            }
        });
    }

    @Override
    public RoomCount queryRoomCount(String placeId) {
        return this.baseMapper.queryRoomCount(placeId);
    }
}
